//
// File:	View.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	21/04/2010 at 09:54:41
// Purpose:	
//
#include "View.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
View::View(float flCenterX /* = 0.0f */, float flCenterY /* = 0.0f */, unsigned int iWidth /* = 0 */, unsigned int iHeight /* = 0 */)
{
	m_iRectSize[0] = iWidth;
	m_iRectSize[1] = iHeight;
	m_flCenter[0] = flCenterX;
	m_flCenter[1] = flCenterY;
}

View::~View()
{
}

//------------------------------------------------------------------
// Center
//------------------------------------------------------------------
void View::SetViewCenterX(float flCenterX)
{
	m_flCenter[0] = flCenterX;
}

void View::SetViewCenterY(float flCenterY)
{
	m_flCenter[1] = flCenterY;
}

float View::GetViewCenterX() const
{
	return m_flCenter[0];
}

float View::GetViewCenterY() const
{
	return m_flCenter[1];
}

//------------------------------------------------------------------
// Rect Size
//------------------------------------------------------------------
void View::SetViewWidth(unsigned int iWidth)
{
	m_iRectSize[0] = iWidth;
}

void View::SetViewHeight(unsigned int iHeight)
{
	m_iRectSize[1] = iHeight;
}

unsigned int View::GetViewWidth() const
{
	return m_iRectSize[0];
}

unsigned int View::GetViewHeight() const
{
	return m_iRectSize[1];
}

//------------------------------------------------------------------
// Inside Tests
//------------------------------------------------------------------
bool View::IsPointInside(float flX, float flY, float flBoundMultiplier /* = 1.0f */) const
{
	float flViewMins[2], flViewMaxs[2];
	flViewMins[0] = m_flCenter[0] - static_cast<float>(m_iRectSize[0]) * 0.5f * flBoundMultiplier;
	flViewMins[1] = m_flCenter[1] - static_cast<float>(m_iRectSize[1]) * 0.5f * flBoundMultiplier;
	flViewMaxs[0] = m_flCenter[0] + static_cast<float>(m_iRectSize[0]) * 0.5f * flBoundMultiplier;
	flViewMaxs[1] = m_flCenter[1] + static_cast<float>(m_iRectSize[1]) * 0.5f * flBoundMultiplier;

	//Trace::debug << "Mins:\t" << flViewMins[0] << "\t" << flViewMins[1] << std::endl;
	//Trace::debug << "Maxs:\t" << flViewMaxs[0] << "\t" << flViewMaxs[1] << std::endl;
	//Trace::debug << "Test:\t" << flX << "\t" << flY << std::endl;

	return (flX >= flViewMins[0] && flX < flViewMaxs[0] && flY >= flViewMins[1] && flY < flViewMaxs[1]);
}

bool View::IsRectInside(float flCenterX, float flCenterY, unsigned int iWidth, unsigned int iHeight, float flBoundMultiplier /* = 1.0f */) const
{
	float flViewMins[2], flViewMaxs[2];
	flViewMins[0] = m_flCenter[0] - static_cast<float>(m_iRectSize[0]) * 0.5f * flBoundMultiplier;
	flViewMins[1] = m_flCenter[1] - static_cast<float>(m_iRectSize[1]) * 0.5f * flBoundMultiplier;
	flViewMaxs[0] = m_flCenter[0] + static_cast<float>(m_iRectSize[0]) * 0.5f * flBoundMultiplier;
	flViewMaxs[1] = m_flCenter[1] + static_cast<float>(m_iRectSize[1]) * 0.5f * flBoundMultiplier;
	float flRectMins[2], flRectMaxs[2];
	flRectMins[0] = flCenterX - static_cast<float>(iWidth) * 0.5f;
	flRectMins[1] = flCenterY - static_cast<float>(iHeight) * 0.5f;
	flRectMaxs[0] = flCenterX + static_cast<float>(iWidth) * 0.5f;
	flRectMaxs[1] = flCenterY + static_cast<float>(iHeight) * 0.5f;
	return !(flRectMaxs[0] < flViewMins[0] || flRectMins[0] > flViewMaxs[0] || flRectMaxs[1] < flViewMins[1] || flRectMins[1] > flViewMaxs[1]);
}
